#include <windows.h>
#include "resource.h"
#include <stdio.h>

extern DWORD scr_cx_res;
extern DWORD scr_cy_res;
extern DWORD scr_bpp;
extern DWORD scr_freq;

BOOL CALLBACK dpStart(HWND hdlg, UINT message, WPARAM wparam, LPARAM lparam)
{
	DWORD disp_index;
	DEVMODE dv;
	char text_dev_mode[128];

	DWORD cur_sel;

	switch (message)
	{
	case WM_INITDIALOG:
		disp_index = 0;

		while (EnumDisplaySettings(NULL, disp_index++, &dv))
		{
			wsprintf(text_dev_mode, "%dx%dx%dbpp %dHz", dv.dmPelsWidth, dv.dmPelsHeight, dv.dmBitsPerPel, dv.dmDisplayFrequency);
			SendDlgItemMessage(hdlg, IDC_DISPLAY, CB_ADDSTRING, 0, (LPARAM) text_dev_mode);
		}

		EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dv);
		wsprintf(text_dev_mode, "%dx%dx%dbpp %dHz", dv.dmPelsWidth, dv.dmPelsHeight, dv.dmBitsPerPel, dv.dmDisplayFrequency);
		cur_sel = (DWORD) SendDlgItemMessage(hdlg, IDC_DISPLAY, CB_FINDSTRING, 0, (LPARAM) text_dev_mode);
		SendDlgItemMessage(hdlg, IDC_DISPLAY, CB_SETCURSEL, cur_sel, 0);
		return FALSE;

	case WM_COMMAND:
		switch (LOWORD(wparam))
		{
		case IDCANCEL:
			EndDialog(hdlg, 0);
			break;

		case IDOK:
			cur_sel = (DWORD) SendDlgItemMessage(hdlg, IDC_DISPLAY, CB_GETCURSEL, 0, 0);
			SendDlgItemMessage(hdlg, IDC_DISPLAY, CB_GETLBTEXT, cur_sel, (LPARAM) text_dev_mode);
			sscanf_s(text_dev_mode, "%d%*c%d%*c%d%*s %d%*s", &scr_cx_res, &scr_cy_res, &scr_bpp, &scr_freq);
			EndDialog(hdlg, 1);
			break;
		}

		return TRUE;
	}

	return FALSE;
}